package com.xinshujia.service.impl;

import com.xinshujia.dao.FunctionDao;
import com.xinshujia.entity.FunctionEntity;
import com.xinshujia.entity.ThresholdEntity;
import com.xinshujia.service.FunctionService;
import com.xinshujia.utils.FunctionConstants;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.time.LocalDateTime;

/**
 * *************************************
 * *   @author: 野狼上校               **
 * *   @since: 2024-07-23 18:44       **
 * *   @version: 1.0                  **
 * *************************************
 * <p>
 */
@Service
public class FunctionServiceImpl implements FunctionService {
    private final FunctionDao functionDao;

    public FunctionServiceImpl(FunctionDao functionDao) {
        this.functionDao = functionDao;
    }

    /**
     * 预警操作
     */
    @Transactional
    @Override
    public boolean alarmOperate(String deviceGuid, int operateType) {
        // 完成的标识
        boolean finishFlag = false;
        // 关闭采集
        boolean closeFlag = insertFunction(deviceGuid, FunctionConstants.FUNCTION_CLOSE_COLLECT);
        // 预警操作
        if (closeFlag) {
            switch (operateType) {
                case 0: // 关闭预警
                    boolean closeFlag_X = insertFunction(deviceGuid, FunctionConstants.FUNCTION_CLOSE_ALARM_X);
                    boolean closeFlag_Y = insertFunction(deviceGuid, FunctionConstants.FUNCTION_CLOSE_ALARM_Y);
                    boolean closeFlag_z = insertFunction(deviceGuid, FunctionConstants.FUNCTION_CLOSE_ALARM_Z);
                    if (closeFlag_X && closeFlag_Y && closeFlag_z) {
                        finishFlag = true;
                    }
                    break;
                case 1: // 开启预警
                    boolean openFlag_X = insertFunction(deviceGuid, FunctionConstants.FUNCTION_OPEN_ALARM_X);
                    boolean openFlag_Y = insertFunction(deviceGuid, FunctionConstants.FUNCTION_OPEN_ALARM_Y);
                    boolean openFlag_z = insertFunction(deviceGuid, FunctionConstants.FUNCTION_OPEN_ALARM_Z);
                    if (openFlag_X && openFlag_Y && openFlag_z) {
                        finishFlag = true;
                    }
                    break;
                case 2: // 清除预警
                    boolean clearFlag = insertFunction(deviceGuid, FunctionConstants.FUNCTION_CLEAR_ALARM);
                    if (clearFlag) {
                        finishFlag = true;
                    }
                default:
                    break;
            }
        }
        if (finishFlag) {
            // 开启采集
            return insertFunction(deviceGuid, FunctionConstants.FUNCTION_OPEN_COLLECT);
        }
        return false;
    }

    /**
     * 设置或修改阈值
     */
    @Override
    public boolean thresholdSet(ThresholdEntity thresholdEntity) {
        String deviceGuid = thresholdEntity.getDeviceGuid();
        // 关闭采集
        boolean closeFlag = insertFunction(deviceGuid, FunctionConstants.FUNCTION_CLOSE_COLLECT);
        if (closeFlag){
            // 设置或修改阈值
            boolean setFlag_X = insertFunction(deviceGuid, FunctionConstants.FUNCTION_UPDATE_THRESHOLD_X + thresholdEntity.getxThreshold() + "}");
            boolean setFlag_Y = insertFunction(deviceGuid, FunctionConstants.FUNCTION_UPDATE_THRESHOLD_Y + thresholdEntity.getyThreshold() + "}");
            boolean setFlag_z = insertFunction(deviceGuid, FunctionConstants.FUNCTION_UPDATE_THRESHOLD_Z + thresholdEntity.getzThreshold() + "}");

            if(setFlag_X && setFlag_Y && setFlag_z){
                // 开启采集
                return insertFunction(deviceGuid, FunctionConstants.FUNCTION_OPEN_COLLECT);
            }
        }
        return false;
    }


    /**
     * 封闭并添加命令到数据库表中（相当于一个新增操作）
     *
     * @param deviceGuid: 设备ID
     * @param functionIn: 操作命令
     */
    private boolean insertFunction(String deviceGuid, String functionIn) {
        FunctionEntity functionEntity = new FunctionEntity();
        functionEntity.setDeviceGuid(deviceGuid);
        functionEntity.setFunctionIn(functionIn);
        functionEntity.setCreateTime(LocalDateTime.now());
        FunctionEntity entity = functionDao.saveAndFlush(functionEntity);
        return !ObjectUtils.isEmpty(entity);
    }
}
